package com.wuzf.springcloud.controller;


import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wuzf.common.entity.CommonResult;
import com.wuzf.springcloud.service.HystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/consumer")
// 全局的fallback属性
//@DefaultProperties(defaultFallback = "global_default_fallback")
public class HystrixController {

    @Autowired
    private HystrixService hystrixService;


    //    @HystrixCommand(fallbackMethod = "default_fallback", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
//    })
    @PostMapping("/order/payment/ok")
    public CommonResult ok() {

        return hystrixService.ok();
    }


    //    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
//    })
    @PostMapping("/payment/error")
    public CommonResult error() {

        return hystrixService.error();
    }


    @PostMapping("/payment/timeout")
    public CommonResult timeout() {

        return hystrixService.timeout();
    }

    @GetMapping("/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
//    })
//    @HystrixCommand
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
//        int age = 10 / 0;
        return hystrixService.paymentInfo_TimeOut(id);
    }


    public CommonResult default_fallback() {

        return new CommonResult(100, "（单独）错误了，哭唧唧/单独再controller中配置的hystrix");
    }

    // 如果没有特别指定，则使用全局的服务降级
    public CommonResult global_default_fallback() {
        return new CommonResult(100, "（全局）错误了，哭唧唧");
    }
}
